home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / QuickTime / ChromaKeyMovie / Sources / menu.c < prev    next >
Encoding:
Text File  |  1995-11-20  |  11.6 KB  |  467 lines  |  [TEXT/MPS ]

  1. /****************************************************/
  2. /*                                                     */
  3. /*    File:    menu.c                                     */
  4. /*                                                    */
  5. /*    Program:    ChromaKeyMovie                         */
  6. /*                                                     */
  7. /*    By:        Jason Hodges-Harris                         */
  8. /*                                                    */
  9. /*    Copyright:    © 1995 by Apple Computer, Inc.,        */ 
  10. /*                    all rights reserved.            */        
  11. /*                                                    */
  12. /****************************************************/
  13.  
  14.  
  15. // Mac Toolbox headers
  16.  
  17. #ifndef __DESK__
  18. #include <Desk.h>
  19. #endif
  20.  
  21. #ifndef __ERRORS__
  22. #include <Errors.h>
  23. #endif
  24.  
  25. #ifndef __MEMORY__
  26. #include <Memory.h>
  27. #endif
  28.  
  29. #ifndef __MENUS__
  30. #include <Menus.h>
  31. #endif
  32.  
  33. #ifndef __QUICKDRAW__
  34. #include <QuickDraw.h>
  35. #endif
  36.  
  37. #ifndef __TOOLUTILS__
  38. #include <ToolUtils.h>
  39. #endif
  40.  
  41. #ifndef __WINDOWS__
  42. #include <Windows.h>
  43. #endif
  44.  
  45.  
  46. // Program headers
  47.  
  48. #ifndef __CHROMAPPHEADER__
  49. #include "ChromaKeyMovie.app.h"
  50. #endif
  51.  
  52. #ifndef __CHROMAPROTOSHEADER__
  53. #include "ChromaKeyMovie.protos.h"
  54. #endif
  55.  
  56.  
  57. //    Global Variables
  58.  
  59. extern short            gKeyMode;
  60. extern Boolean            gDone;                // program loop test condition
  61. extern Boolean            gMovieOpen;
  62. extern Boolean            gMovieBackGrnd;
  63.  
  64.  
  65. extern GWorldPtr        gOffscreenPort,
  66.                         gBackGroundPort,
  67.                         gBackGroundPicture;
  68. extern PixMapHandle        gMoviePixmap,
  69.                         gBackGndPixmap,
  70.                         gBackGndPictPM;
  71.  
  72.  
  73. // Initialise the application menubar.
  74.  
  75. #pragma segment Menu
  76. void MenuBarInit (void)
  77. {
  78.     SetMenuBar (GetNewMBar(rMenuBar));
  79.     AddResMenu (GetMHandle(mApple),'DRVR');
  80.     DrawMenuBar();
  81. }
  82.  
  83.  
  84. // Sets the menubar to the default settings during the application initialisation phase.
  85. // i.e. In the 'Keying Mode' menu, the 'Transparent Color Transfer' menu option has a
  86. // tick mark against it to indicate it as the currently selected mode.
  87.  
  88. #pragma segment Menu
  89. void    DoAdjustMenus(void)
  90. {
  91.     DoAdjustFileMenu();
  92.     DoAdjustEditMenu();
  93.     DoAdjustKeyMode();
  94.     DoAdjustOptions(false,true);    // set the default to loop movie and not play every frame
  95.     DrawMenuBar();
  96.     return;
  97. }
  98.  
  99.  
  100.  
  101. // The DoMenuCommand() function performs all of the handling of 
  102. // the user's interaction with the application
  103.  
  104. #pragma segment Menu
  105. void    DoMenuCommand(long menuResult)
  106. {
  107.     MovieDocHndl        theDocHandle;
  108.     Str255                daName;
  109.     WindowPtr            window,theNextWindow;
  110.     short                menuID,
  111.                         menuItem,
  112.                         daRefNum;
  113.     static Boolean        sLoopMovie = true;            // set the movie to loop as default
  114.     static Boolean        sPlayAllFrames = false;        // set don't play all frames as default
  115.     
  116.     menuID   = HiWord(menuResult);
  117.     menuItem = LoWord(menuResult);
  118.  
  119.     switch (menuID) 
  120.     {
  121.  
  122.         case mApple:                // Apple menubar items
  123.             switch (menuItem) 
  124.             {
  125.                 /* Display the application about box.
  126.                    App name, copyright etc. */
  127.                 case iAbout:        
  128.                     DisplayAlert (rAboutBox,0,0);
  129.                 break;
  130.  
  131.                 // handle all menubar Desk Accessories.
  132.                 default:            
  133.                     GetItem(GetMHandle(mApple), menuItem, daName);
  134.                     daRefNum = OpenDeskAcc(daName);
  135.                 break;
  136.             }
  137.             break;
  138.  
  139.         case mFile:                        // File menubar items
  140.             switch (menuItem) 
  141.             {
  142.                 case iOpen:                // Open movie file
  143.                 /* Do nothing if a window is already open.
  144.                    Currently this application can only handle
  145.                    one open window manily due to processor 
  146.                    requirements in keeping a movie serviced
  147.                    at a high frame rate. */
  148.                 if (!gMovieOpen)
  149.                     {
  150.                          /* initialise environment for movie playback.
  151.                             If an error is encountered, the PlayMovieChroma()
  152.                             function will return false and the application will
  153.                             bail without displaying an error as it should have
  154.                             recovered gracefully. */
  155.                          if (PlayMovieChroma()) 
  156.                         {
  157.                             gMovieOpen = true;    // set boolean to indicate movie window open
  158.                             /* Perform tests to see if menu items to loop and play all
  159.                               frames of the movie are selected and set the movie according */
  160.                             if (sPlayAllFrames)
  161.                                 sPlayAllFrames = SetPlayAllFrames(!sPlayAllFrames);
  162.                             if (sLoopMovie)
  163.                                 sLoopMovie = SetLoopMovie(!sLoopMovie);
  164.  
  165.                             /* Test for which croma key mode is selected and set
  166.                                if the default Transparent Color Transfer mode isn't used. */
  167.                             if (gKeyMode == graphix)
  168.                                 VideoGraphicsMode(FrontWindow(),true);
  169.                             if (gKeyMode == modifierTrax)
  170.                                 ModifierTrackMode(FrontWindow());
  171.                             DoAdjustFileMenu();
  172.                         }
  173.                     }
  174.                 break;
  175.                 case iClose:
  176.                     if (gMovieOpen)
  177.                     {
  178.                         /* close open movie window and reset boolean to indicate
  179.                            that another window can be opened */
  180.                         gMovieOpen = false;
  181.                         DisposeWindowDocs (FrontWindow());
  182.                     }
  183.                 break;
  184.                 case iQuit:
  185.                     /* As quit menu option selected, close the window (if open),
  186.                        and set the loop boolean to true to allow application to exit. */
  187.                     if (gMovieOpen)
  188.                     {
  189.                         window = FrontWindow();
  190.                         while (window)
  191.                         {
  192.                             theNextWindow = &((WindowPeek)window)->nextWindow->port;
  193.                             DisposeWindowDocs (window);
  194.                             window = theNextWindow;
  195.                         }
  196.                         // SetPort(FrontWindow());
  197.                     }
  198.                     gMovieOpen = false;
  199.                     gDone=true;
  200.                 break;
  201.             }
  202.             break;
  203.  
  204.         /*  Edit menubar options. These aren't
  205.             implemented but included for completeness. */
  206.         case mEdit:        
  207.             switch (menuItem) 
  208.             {
  209.                 case iUndo:
  210.                     break;
  211.                 case iCut:
  212.                     break;
  213.                 case iCopy:
  214.                     break;
  215.                 case iPaste:
  216.                     break;
  217.             }
  218.             DoAdjustEditMenu();
  219.         break;
  220.         /* The (Keying Mode) mMode menu, controls the user interaction
  221.            with the different methods of keying a movie 
  222.            and setting up the display options. */
  223.         case mMode:
  224.                 
  225.             window = FrontWindow();
  226.             switch (menuItem)
  227.             {
  228.                 case iKeyColor:
  229.                     /* Sets the color used as transparent to allow the background
  230.                      image to display thro the foreground image and updates
  231.                      the modifier track / graphix modes if either is the selected
  232.                      mode, as these only set the transparent color when initialised. */
  233.                     TransparentColor();    
  234.                     switch (gKeyMode)
  235.                     {
  236.                         case modifierTrax:
  237.                             // update the modifier track information
  238.                         break;
  239.                         case graphix:
  240.                             // update the graphix mode to key out new color
  241.                             VideoGraphicsMode(window,true);    
  242.                         break;
  243.                     }
  244.                 break;
  245.                 /* Set the gMovieBackGrnd variable to position the movie in front or behind of the 
  246.                    background image when using the 'Transparent Color Transfer' mode */
  247.                 case iMovieBack:
  248.                     gMovieBackGrnd = true;
  249.                 break;
  250.                 case iMovieFront:
  251.                     gMovieBackGrnd = false;
  252.                 break;
  253.                 /* 'Transparent Color Transfer' mode selected. If a window isn't open
  254.                     just set the transfer mode global, else check the current
  255.                     mode of the movie window and remove information set by previous mode. */
  256.                 case iTransparent:
  257.                     if (gMovieOpen)
  258.                     {
  259.                         theDocHandle=(MovieDocHndl)GetWRefCon((WindowPtr)window);
  260.                         switch (gKeyMode)
  261.                         {
  262.                             case modifierTrax:
  263.                                 HLock((Handle)theDocHandle);
  264.                                 if (DestroyModifierTrack(theDocHandle))
  265.                                     DisplayAlert (rGenAlert,rErrMessages,4);
  266.                                 HUnlock((Handle)theDocHandle);
  267.                             break;
  268.                             case graphix:
  269.                                 VideoGraphicsMode(window,false);
  270.                             break;
  271.                             
  272.                         }
  273.                         SetMovieGWorld((**theDocHandle).theMovie,gOffscreenPort,nil);
  274.                     }
  275.                     gKeyMode = transparentMode;
  276.                 break;
  277.                 /* 'Graphix Mode' mode selected. If a window isn't open
  278.                     just set the transfer mode global, else check the current
  279.                     mode of the movie window and remove information set by previous mode. */
  280.                 case iGraphix:
  281.                     if (gMovieOpen)
  282.                     {
  283.                         theDocHandle=(MovieDocHndl)GetWRefCon((WindowPtr)window);
  284.                         switch (gKeyMode)
  285.                         {
  286.                             case modifierTrax:
  287.                                 HLock((Handle)theDocHandle);
  288.                                 if (DestroyModifierTrack(theDocHandle))
  289.                                     DisplayAlert (rGenAlert,rErrMessages,4);
  290.                                 HUnlock((Handle)theDocHandle);
  291.                             break;
  292.                         }
  293.                         VideoGraphicsMode(window,true);    // use VideoMediaGraphicsMode. See IM QuickTime 2:287
  294.                     }
  295.                     gKeyMode = graphix;
  296.                 break;
  297.                 /* 'Modifier Track' mode selected. If a window isn't open
  298.                     just set the transfer mode global, else check the current
  299.                     mode of the movie window and remove information set by previous mode. */
  300.                 case iModifier:
  301.                     if (gMovieOpen)
  302.                     {
  303.                         theDocHandle=(MovieDocHndl)GetWRefCon((WindowPtr)window);
  304.                         switch (gKeyMode)
  305.                         {
  306.                             case graphix:
  307.                                 VideoGraphicsMode(window,false);
  308.                             break;
  309.                         }
  310.                         ModifierTrackMode(window);
  311.                     }
  312.                     gKeyMode = modifierTrax;
  313.                 break;
  314.             }
  315.             DoAdjustKeyMode();
  316.         break;
  317.         /* Options menu selected. These options set the playback options of the movie.
  318.            The available options are currently looping and play every frame. Note
  319.            that the Play Every Frame option mutes any active audio tracks when selected. */
  320.         case mOptions:
  321.             switch (menuItem)
  322.             {
  323.                 case iEveryFrame:
  324.                     sPlayAllFrames = SetPlayAllFrames(sPlayAllFrames);
  325.                 break;
  326.                 case iLoopMovie:
  327.                     sLoopMovie = SetLoopMovie(sLoopMovie);
  328.                 break;
  329.             }
  330.             DoAdjustOptions(sPlayAllFrames,sLoopMovie);
  331.         break;
  332.     }
  333.     HiliteMenu(0);        // Unhighlight what MenuSelect (or MenuKey) hilited.
  334. }
  335.  
  336.  
  337. // Adjust the file menubar items
  338.  
  339. #pragma segment Menu
  340. void    DoAdjustFileMenu(void)
  341. {
  342.     MenuHandle            menu;
  343.  
  344.     /* Enable the Open and Close File menu items dependent
  345.      on whether a window is currently open */
  346.     menu = GetMHandle(mFile);
  347.     if (gMovieOpen)
  348.     {
  349.         DisableItem(menu,iOpen);
  350.         EnableItem(menu,iClose);
  351.     }
  352.     else if (!gMovieOpen)
  353.     {
  354.         EnableItem(menu,iOpen);
  355.         DisableItem(menu,iClose);
  356.     }
  357.     return;
  358. }
  359.  
  360.  
  361. /* Adjust the Edit menu bar items. Currently as these items
  362.    are disabled as they're not supported. */
  363.  
  364. #pragma segment Menu
  365. void    DoAdjustEditMenu(void)
  366. {
  367.     MenuHandle        menu;
  368.     short            i;
  369.     
  370.     menu = GetMHandle(mEdit);
  371.     for (i = iUndo; i <= iPaste; ++i)
  372.         DisableItem(menu, i);
  373.     return;
  374. }
  375.  
  376.  
  377. /* Sets the Keying Mode menu items. This is responsible for setting
  378.    check marks against the currently selected transfer method and
  379.    the position of the movie relative to the background. It also
  380.    enables or disables the movie position items when the graphix
  381.    and modifierTrax methods are used, as these both display in the foreground. */
  382.  
  383. #pragma segment Menu
  384. void    DoAdjustKeyMode(void)
  385. {
  386.     MenuHandle        menu;
  387.     short            itemMark;
  388.     
  389.     menu = GetMHandle(mMode);
  390.     switch (gKeyMode)
  391.     {
  392.         case transparentMode:
  393.             GetItemMark(menu,iTransparent,&itemMark);
  394.             if (itemMark == 0x00)
  395.             {
  396.                 SetItemMark(menu,iTransparent,0x12);
  397.                 SetItemMark(menu,iGraphix,0x00);
  398.                 SetItemMark(menu,iModifier,0x00);
  399.                 EnableItem(menu,iMovieBack);
  400.                 EnableItem(menu,iMovieFront);
  401.             }
  402.         break;
  403.         case graphix:
  404.             GetItemMark(menu,iGraphix,&itemMark);
  405.             if (itemMark == 0x00)
  406.             {
  407.                 SetItemMark(menu,iTransparent,0x00);
  408.                 SetItemMark(menu,iGraphix,0x12);
  409.                 SetItemMark(menu,iModifier,0x00);
  410.                 DisableItem(menu,iMovieBack);
  411.                 DisableItem(menu,iMovieFront);
  412.             }
  413.         break;
  414.         case modifierTrax:
  415.             GetItemMark(menu,iModifier,&itemMark);
  416.             if (itemMark == 0x00)
  417.             {
  418.                 SetItemMark(menu,iTransparent,0x00);
  419.                 SetItemMark(menu,iGraphix,0x00);
  420.                 SetItemMark(menu,iModifier,0x12);
  421.                 DisableItem(menu,iMovieBack);
  422.                 DisableItem(menu,iMovieFront);
  423.             }
  424.         break;
  425.     }
  426.     GetItemMark(menu,iMovieBack,&itemMark);
  427.     if (gMovieBackGrnd)
  428.     {
  429.         if (itemMark == 0x00)
  430.         {
  431.             SetItemMark(menu,iMovieBack,0x12);
  432.             SetItemMark(menu,iMovieFront,0x00);
  433.         }
  434.     }
  435.     else
  436.     {
  437.         if (itemMark != 0x00)
  438.         {
  439.             SetItemMark(menu,iMovieBack,0x00);
  440.             SetItemMark(menu,iMovieFront,0x12);
  441.         }
  442.     }
  443.     return;
  444. }
  445.  
  446.  
  447.  
  448. /* DoAdjustOptions() adjusts the Options menu items and 
  449.    placed / removes check marks against these items when
  450.    they're selected / deselected. */
  451.  
  452. #pragma segment Menu
  453. void DoAdjustOptions(Boolean playAllFrames, Boolean loopMovie)
  454. {
  455.     MenuHandle        menu;
  456.     
  457.     menu = GetMHandle(mOptions);
  458.     if (playAllFrames)
  459.         SetItemMark(menu,iEveryFrame,0x12);
  460.     else
  461.         SetItemMark(menu,iEveryFrame,0x00);
  462.     if (loopMovie)
  463.         SetItemMark(menu,iLoopMovie,0x12);
  464.     else
  465.         SetItemMark(menu,iLoopMovie,0x00);
  466.     return;
  467. }